package com.yy.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yy.model.CmsMessageEntity;
import com.yy.service.CmsMessageService;
import com.yy.utils.PageUtils;
import com.yy.utils.Query;
import com.yy.utils.ResponseEntity;
import com.yy.validator.Assert;
import com.yy.validator.ValidatorUtils;
import com.yy.validator.group.AddGroup;
import com.yy.validator.group.UpdateGroup;


/**
 * 在线留言
 * 
 * @author yy
 * @email 
 * @date 2017-07-26 16:26:46
 */
@RestController
@RequestMapping("cmsmessage")
public class CmsMessageController {
	@Autowired
	private CmsMessageService cmsMessageService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("cmsmessage:list")
	public ResponseEntity list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<CmsMessageEntity> cmsMessageList = cmsMessageService.queryList(query);
		int total = cmsMessageService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(cmsMessageList, total, query.getLimit(), query.getPage());
		
		return ResponseEntity.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("cmsmessage:info")
	public ResponseEntity info(@PathVariable("id") Long id){
		CmsMessageEntity cmsMessage = cmsMessageService.queryObject(id);
		
		return ResponseEntity.ok().put("cmsMessage", cmsMessage);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	//@RequiresPermissions("cmsmessage:save")
	public ResponseEntity save(@RequestBody CmsMessageEntity cmsMessage){
		ValidatorUtils.validateEntity(cmsMessage, AddGroup.class);
		Assert.isBlank(cmsMessage.getName(), "姓名不能为空");
		Assert.isBlank(cmsMessage.getMobile(), "姓名不能为空");
		
		cmsMessageService.save(cmsMessage);
		
		return ResponseEntity.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("cmsmessage:update")
	public ResponseEntity update(@RequestBody CmsMessageEntity cmsMessage){
		ValidatorUtils.validateEntity(cmsMessage, UpdateGroup.class);
		cmsMessageService.update(cmsMessage);
		
		return ResponseEntity.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("cmsmessage:delete")
	public ResponseEntity delete(@RequestBody Long[] ids){
		cmsMessageService.deleteBatch(ids);
		
		return ResponseEntity.ok();
	}
	
}
